<?php

	require_once 'config.php';
	//PADRÃO type=value[[&task=value][&data={...}]]
	if(isset($_POST['type'])){
	
		$task = $data = null;
		$type = $_POST['type'];
		
		if(isset($_POST['task'])) $task = $_POST['task'];
		if(isset($_POST['data'])){
			$data=str_replace("\\", "\\\\", $_POST['data']);
			$data=cleanData(json_decode($_POST['data'],true));
		} 

		//$data = cleanData(json_decode($_POST['data'],true));
		//if($task == 'sendMessage') file_put_contents('data', $data);
		
		$className = ucfirst($type);
		
		$obj = new $className();
		$obj->$task($data);
	}

	function __autoload($class){
		require_once "php/{$class}.php";
	}

	function cleanData($data){
		foreach ($data as $key => $value) {
			if(is_string($value)){

		//		require_once ROOT."php/class/Database.class";
		//		$db = new Database;
		//		$val=mysql_real_escape_string($value);
				$value=str_replace("'", "`", $value);
				$value=str_replace("<", "&#60", $value);
				$value=str_replace("\\", "\\\\", $value);
				
				$data[$key]=$value;
			}else if(is_array($value)){
				$data[$key]=cleanData($value);
			}
		}

		return $data;
	}
?>